package cn.tangwei.user.controller;

import cn.tangwei.basic.exception.BusinessException;
import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.user.dto.LoginDto;
import cn.tangwei.user.service.ILoginService;
import jdk.nashorn.api.scripting.AbstractJSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 账号登录流程
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxRusult account(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map =  loginService.account(loginDto);
            return AjaxRusult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("系统繁忙，稍后再试");
        }
    }

    /**
     * 微信登录登录流程
     * @param params
     * @return
     */
    @PostMapping("/wechat")
    public AjaxRusult wechat(@RequestBody Map<String, String> params){
        try {
             return loginService.wechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("系统繁忙，稍后再试");
        }
    }

    /**
     * 微信登录绑定流程
     * @param params
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxRusult binder(@RequestBody Map<String, String> params){
        try {
            Map<String, Object> map =  loginService.binder(params);
            return AjaxRusult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("系统繁忙，稍后再试");
        }
    }

}
